package com.nzy.excel;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;

/**
 * @author jukg
 * @date 2023/04/13 13:35
 * @description
 */
public class TestEasyExcel {

    public static void main2(String[] args) {
        //实现excel写操作
        // 1设置写入文件夹地址和excel文件名称
        String filename="G:\\XY\\learning\\xycode\\src\\main\\java\\com\\nzy\\excel\\demo.xlsx";
        List<String> title = new ArrayList<>();
        title.addAll(Arrays.asList("测试1", "测试2", "测试3", "测试4"));
        List<List<String>> head = new ArrayList<>();
        for(String t : title){
            head.add(Arrays.asList(t));
        }
        List<String> data = new ArrayList<>();
        data.addAll(Arrays.asList(new String[]{"1","2","3","4"}));
        data.addAll(Arrays.asList(new String[]{"1","2","3","4"}));
        //2 调用easyexcel里面的方法实现写操作
        EasyExcel.write(filename).registerWriteHandler(new AbstractColumnWidthStyleStrategy(){

            @Override
            protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
                Sheet sheet = writeSheetHolder.getSheet();
                sheet.setColumnWidth(cell.getColumnIndex(), 15000);
            }
        }).head(head).sheet("学生列表").doWrite(data);
    }


    public static void main1(String[] args) {
        List<ExcelExportEntity> colList = new ArrayList<>();
        List<Map<String, String>> data= new ArrayList<>();

        ExcelExportEntity entity = new ExcelExportEntity("姓名", "name", 40);
        ExcelExportEntity entity2 = new ExcelExportEntity("账号", "id", 40);
        ExcelExportEntity entity3 = new ExcelExportEntity("332", "test", 40);
        colList.add(entity);
        colList.add(entity2);
        colList.add(entity3);


        for(int i=0; i< 5; i++){
            Map<String, String> map = new HashMap<>();
            map.put("name", i+"张三");
            map.put("id", i+"");
            map.put("test", i+"====");
            data.add(map);
        }

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),
                colList, data);
        try {
            //先创建父文件夹
            File file = FileUtil.file("C:\\Users\\Administrator\\Desktop\\aa.xls");
            FileOutputStream fos = new FileOutputStream(file);
            workbook.write(fos);
            fos.close();
        } catch (IOException e) {
           e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        List<Vo> result = new ArrayList<>();

        List<Map<String, Object>> list  = new ArrayList<>();//模拟数据库查询结果

        for(Map<String, Object> map : list){
            String provinceId = (String)map.remove("province_id");
            for(String key : map.keySet()){
                Vo vo = new Vo();
                vo.setId(provinceId);
                vo.setKey(key);
                vo.setValue((Long)map.get(key));
                result.add(vo);
            }
        }
        //return result;
    }
}
class Vo implements Serializable{
    private String id;
    private String key;
    private Long value;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Long getValue() {
        return value;
    }

    public void setValue(Long value) {
        this.value = value;
    }
}
